package com.dai.designpattern.prototype.practice.mold;

import java.io.Serializable;
import java.util.regex.Pattern;

/**
 * 场景匹配词
 *
 * @author daijiyong
 */
public class SceneMatchWord implements Serializable {

    // 匹配规则
    private Pattern rule;

    public SceneMatchWord(Pattern rule) {
        this.rule = rule;
    }

    /**
     * 根据用户输入内容进行匹配
     * 匹配成功返回true，失败返回false
     *
     * @param userInput 用户输入
     * @return 匹配成功或失败
     */
    public Boolean match(String userInput) {
        return matchRule(userInput);
    }


    private boolean matchRule(String str) {
        if (rule == null) {
            return false;
        }
        return rule.matcher(str).find();
    }

    public Pattern getRule() {
        return rule;
    }

    public void setRule(Pattern rule) {
        this.rule = rule;
    }
}
